package com.simple.piger.controller.company;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.simple.piger.component.CommonComponent;
import com.simple.piger.controller.BaseController;
import com.simple.piger.dto.req.InviteCompanyEmpReqDto;
import com.simple.piger.dto.resp.company.InviteCompanyEmpRespDto;
import com.simple.piger.entity.InviteCompany;
import com.simple.piger.entity.InviteCompanyEmp;
import com.simple.piger.enums.InviteCompanyScaleEnums;
import com.simple.piger.service.InviteCompanyEmpService;
import com.simple.piger.service.InviteCompanyService;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.DateUtils;
import com.simple.piger.utils.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 公司职员 前端控制器
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/inviteCompanyEmp")
public class InviteCompanyEmpController extends BaseController {
    @Autowired
    private CommonComponent commonComponent;
    @Autowired
    private InviteCompanyEmpService inviteCompanyEmpService;
    @Autowired
    private InviteCompanyService inviteCompanyService;

    /**
     * 列出 公司职员列表的接口
     * @param hashMap
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        String name = jsonObject.getString("name");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        List<InviteCompanyEmpRespDto> list = inviteCompanyEmpService.list(page, limit, name);
        int count = inviteCompanyEmpService.count(name);
        return success(count, list);
    }

    /**
     * 列出  公司详情的接口
     * @param companyEmpId
     * @return
     */
    @GetMapping("/detail/{companyEmpId}")
    public AjaxResult detail(@PathVariable("companyEmpId") String companyEmpId) {
        InviteCompanyEmp inviteCompanyEmp = inviteCompanyEmpService.selectById(companyEmpId);
        InviteCompany inviteCompany = inviteCompanyService.selectById(inviteCompanyEmp.getInviteCompanyId());
        InviteCompanyEmpRespDto dto = new InviteCompanyEmpRespDto();
        BeanUtils.copyProperties(inviteCompanyEmp, dto);
        dto.setCompanyImgUrl(inviteCompany.getImgUrl());
        dto.setCompanyName(inviteCompany.getCompanyName());
        return success(dto);
    }

    /**
     * 运营平台查看公司职员的接口
     *
     * @param hashMap
     * @return
     */
    @GetMapping("/platformCompanyEmpList")
    public AjaxResult platformCompanyList(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String companyId = commonComponent.getCompanyIdByCmsUserId(getValueFromSession("userId"));
        List<InviteCompanyEmpRespDto> list = inviteCompanyEmpService.platformCompanyEmpList(page, limit, companyId);
        int count = inviteCompanyEmpService.platformCompanyEmpListCount(companyId);
        return success(count, list);
    }

    /**
     * 运营平台查看公司职员详情的接口
     * @param id
     * @return
     */
    @GetMapping("/platformCompanyEmpDetail/{id}")
    public AjaxResult platformCompanydetail(@PathVariable("id")String id) {
        InviteCompanyEmp inviteCompanyEmp = inviteCompanyEmpService.selectById(id);
        InviteCompany inviteCompany = inviteCompanyService.selectById(inviteCompanyEmp.getInviteCompanyId());
        InviteCompanyEmpRespDto dto = new InviteCompanyEmpRespDto();
        BeanUtils.copyProperties(inviteCompanyEmp, dto);
        dto.setCompanyImgUrl(inviteCompany.getImgUrl());
        dto.setCompanyName(inviteCompany.getCompanyName());
        dto.setScaleValue(InviteCompanyScaleEnums.transfer(inviteCompany.getScale()));
        dto.setFinancingValue(InviteCompanyScaleEnums.transfer(inviteCompany.getFinancing()));
        dto.setAddress(inviteCompany.getAddress());
        dto.setCompanyImgUrl(inviteCompany.getImgUrl());
        dto.setIndustry(inviteCompany.getIndustry());
        dto.setCreateDate(DateUtils.dateFormat(dto.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return success(dto);
    }

    /**
     * 删除公司职员的接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        InviteCompanyEmp inviteCompanyEmp = inviteCompanyEmpService.selectById(id);
        inviteCompanyEmp.setDeleteFlag("1");
        inviteCompanyEmp.setUpdateTime(new Date());
        inviteCompanyEmp.updateById();
        return success();
    }

    /**
     * 更新公司职员的接口
     * @param hashMap
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        InviteCompanyEmpReqDto reqDto = JSONObject.toJavaObject(jsonObject, InviteCompanyEmpReqDto.class);
        inviteCompanyEmpService.updateEmp(reqDto);
        return success();
    }

    /**
     * 保存或者更新公司职员的接口
     * @param hashMap
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        InviteCompanyEmpReqDto reqDto = JSONObject.toJavaObject(jsonObject, InviteCompanyEmpReqDto.class);
        inviteCompanyEmpService.saveOrUpdate(reqDto);
        return success();
    }
}


